﻿using System;
using System.Windows.Forms;

namespace GUI
{
    public partial class OpenExistingForm : Form
    {
        public event MountEventHandler MountEvent;

        public OpenExistingForm()
        {
            InitializeComponent();
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (MountEvent != null)
                    MountEvent(driveLetterCBox.Items[driveLetterCBox.SelectedIndex].ToString()[0], passwordTBox.Text, openFileDialog.FileName);
                passwordTBox.Clear();
                driveLetterCBox.SelectedIndex++;
                Close();
            }
            catch (Exception)
            {
                Dispose();
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            try
            {
                Close();
            }
            catch (Exception)
            {
                Dispose();
            }
        }

        private void passwordTBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                okBtn_Click(sender, e);
            }
        }

        private void OpenExistingForm_Load(object sender, EventArgs e)
        {
            try
            {
                driveLetterCBox.DataSource = DriveManagement.DriveManager.FreeLetters;
                if (openFileDialog.ShowDialog() != DialogResult.OK)
                {
                    Close();
                }
            }
            catch (Exception)
            {
                Dispose();
            }
        }
    }
}
